package com.example.hanfu.login;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.hanfu.R;
import com.example.hanfu.ViewActivity;


public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "RegisterActivity";
    LoginDBManager manager = new LoginDBManager(RegisterActivity.this);
    TextView register_id;
    TextView register_pwd;
    Button register;
    Button login;
    LoginState loginState=new LoginState();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_id=findViewById(R.id.registerid);
        register_pwd=findViewById(R.id.registerpwd);
        register=findViewById(R.id.regi_register);
        login=findViewById(R.id.regi_login);

    }
    public void login(View v){
        //跳转到登陆页面
        Intent open=new Intent(this, LoginActivity.class);
        startActivity(open);

    }
    public void register(View v){
        String number_str=register_id.getText().toString();
        String password=register_pwd.getText().toString();
        Log.i(TAG,"注册名："+number_str);
        Log.i(TAG,"密码："+password);



        if(number_str.length()==0||password.length()==0){
            //提示输入
            new AlertDialog.Builder(RegisterActivity.this)
                    .setTitle("提示")
                    .setMessage("请输入完整注册信息")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });

        }else {

            //向数据库插入数据
            manager.add(new Login(number_str, password));
            Login num=manager.findById(number_str);

            Log.i(TAG,"插入数据库:"+num.getPwd());
            //注册成功，跳转到主页
            loginState.setLogin_state(1);
            loginState.setUserName(number_str);

            Intent open=new Intent(this, ViewActivity.class);
            startActivity(open);
        }



    }
}